public class VideoMessage extends Message{

    public VideoMessage(TIMMessage message){
        this.message = message;
    }

    public VideoMessage(String filePath, String coverPath, long duration){
        message = new TIMMessage();
        TIMUGCElem elem = new TIMUGCElem();
        TIMUGCCover cover = new TIMUGCCover();
        File file = new File(coverPath);
        int height = 0, width = 0;
        if (file.exists()) {
            final BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeFile(coverPath, options);
            height = options.outHeight;
            width = options.outWidth;
        }
        cover.setHeight(height);
        cover.setWidth(width);
        cover.setType("PNG");
        TIMUGCVideo video = new TIMUGCVideo();
        video.setType("MP4");
        video.setDuration(duration);
        elem.setCover(cover);
        elem.setVideo(video);
        elem.setVideoPath(filePath);
        elem.setCoverPath(coverPath);
        message.addElement(elem);
    }

    @Override
    public void showMessage(final ChatAdapter.ViewHolder viewHolder, final Context context) {
        clearView(viewHolder,true,context);
        if (checkRevoke(viewHolder)) return;
        final TIMUGCElem e = (TIMUGCElem) message.getElement(0);
        switch (message.status()){
            case Sending:
                showSnapshot("00:00",viewHolder,e.getCoverPath(),context,true);
                break;
            case SendSucc:
                final TIMUGCCover snapshot = e.getCover();
                final String time=formatLongTime(e.getVideo().getDuration());
                if (FileUtil.isCacheFileExist(e.getFileId())){
                    showFirstP(time,viewHolder,e.getCoverPath(),context,false);
                }else{
                    snapshot.getImage(FileUtil.getCacheFilePath(e.getFileId()), new TIMCallBack() {
                        @Override
                        public void onError(int i, String s) {
                        }
                        @Override
                        public void onSuccess() {
                            showFirstP(time,viewHolder, e.getCoverPath(),context,false);
                        }
                    });
                }
                final String fileName = e.getFileId()+"_video";
                if (!FileUtil.isCacheFileExist(fileName)) {
                    e.getVideo().getVideo(FileUtil.getCacheFilePath(fileName), new TIMCallBack() {
                        @Override
                        public void onError(int i, String s) {
                        }
                        @Override
                        public void onSuccess() {
                            setVideoEvent(viewHolder,fileName,context);
                        }
                    });
                }else{
                    setVideoEvent(viewHolder,fileName,context);
                }
                break;
        }
        showStatus(viewHolder);
    }

    @Override
    public String getSummary() {
        return MyApplication.getContext().getString(R.string.summary_video);
    }

    @Override
    public void save() {
    }


    /**
     * 显示缩略图
     */
    private void showFirstP(final String time,final ChatAdapter.ViewHolder viewHolder,final String path,final Context context,boolean issending){
        if (path == null) return;

        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(path, options);
        int tW,tH, width=options.outWidth,height=options.outHeight;
        if(width<height){
            tW=PxUtil.ChatImageviewS;
            tH=PxUtil.ChatImageviewB;
        }else {
            tH=PxUtil.ChatImageviewS;
            tW=PxUtil.ChatImageviewB;
        }
        RelativeLayout relativeLayout = new RelativeLayout(MyApplication.getContext());
        RelativeLayout.LayoutParams layoutParams=new RelativeLayout.LayoutParams(tW, tH);
        if(message.isSelf()){
            layoutParams.setMargins(0,0,PxUtil.ChatImageviewMa,0);
        }else {
            layoutParams.setMargins(PxUtil.ChatImageviewMa,0,0,0);
        }
        relativeLayout.setLayoutParams(layoutParams);
            RoundImageView imageView = new RoundImageView(MyApplication.getContext());
            Glide.with( context ).load( path ).thumbnail( 0.1f ).into( imageView );
        relativeLayout.addView(imageView);

        if(issending){
            RelativeLayout.LayoutParams layoutParams2=new RelativeLayout.LayoutParams(PxUtil.barWH,PxUtil.barWH);
            layoutParams2.addRule(RelativeLayout.CENTER_IN_PARENT);
            ProgressBar progressBar=new ProgressBar(MyApplication.getContext());
            progressBar.setLayoutParams(layoutParams2);
            progressBar.setIndeterminateDrawable(context.getResources().getDrawable(R.drawable.indeterminate_drawable));
            relativeLayout.addView(progressBar);
        }else {
            RelativeLayout relativeLayout3 = new RelativeLayout(MyApplication.getContext());
            RelativeLayout.LayoutParams layoutParams3=new RelativeLayout.LayoutParams( PxUtil.ChatViewPlay,PxUtil.ChatViewPlay);
            layoutParams3.addRule(RelativeLayout.CENTER_IN_PARENT);
            relativeLayout3.setLayoutParams(layoutParams3);

            View view=new View(MyApplication.getContext());
            view.setBackgroundResource(R.drawable.serch_video_yuan);
            RelativeLayout.LayoutParams layoutParams4=new RelativeLayout.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT);
            view.setLayoutParams(layoutParams4);

            ImageView imageView2 = new ImageView(MyApplication.getContext());
            imageView2.setImageResource(R.drawable.livewx2);
            RelativeLayout.LayoutParams layoutParams2=new RelativeLayout.LayoutParams( PxUtil.ChatViewPlay/3,PxUtil.ChatViewPlay/3);
            layoutParams2.addRule(RelativeLayout.CENTER_IN_PARENT);
            imageView2.setLayoutParams(layoutParams2);

            relativeLayout3.addView(view);
            relativeLayout3.addView(imageView2);
            relativeLayout.addView(relativeLayout3);
        }

            TextView tv = new TextView(MyApplication.getContext());
            tv.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 11);
            tv.setTextColor(0xffffffff);
            tv.setText(time);
            RelativeLayout.LayoutParams layoutParams6=new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
            layoutParams6.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            layoutParams6.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
            layoutParams6.setMargins(10,10,10,10);
            tv.setLayoutParams(layoutParams6);
            relativeLayout.addView(tv);
        getBubbleView(viewHolder,true,context).addView(relativeLayout);
    }

    private void setVideoEvent(final ChatAdapter.ViewHolder viewHolder, final String fileName,final Context context){
        getBubbleView(viewHolder,true,context).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String path=FileUtil.getCacheFilePath(fileName);
                if (context == null) return;
                final TIMUGCElem e = (TIMUGCElem) message.getElement(0);
                File file = new File(path);
                if (file.length() < e.getVideo().getSize()) return;
                Intent intent = new Intent(context, VideoActivity.class);
                intent.putExtra("path", path);
                context.startActivity(intent);     
            }
        });
    }
   
    public static String formatLongTime(long mss) {
        String DateTimes = "";
        long sb2=mss/1000;
        long minutes = sb2 /60;
        long seconds = sb2 % 60;
        if(seconds<10){
            DateTimes="0"+minutes+":0"+seconds;
        }else {
            DateTimes="0"+minutes+":"+seconds;
        }
        return DateTimes;
    }
}
